;+
; NAME:
;       PROBEVAL_DISCR
;
; PURPOSE:
;       Evaluate various properties of the probability density
;       functions, and place them into thingies for return.
;
; CATEGORY:
;       distance-omnibus
;
; CALLING SEQUENCE:
;       discr_str = probeval_discr, dist, pvec, dtan
;
; INPUTS:
;       DIST -- Distance array to be used
;       PVEC -- Probability array
;       DTAN -- Tangent distance associated with this source
;
; OPTIONAL INPUTS:
;       NONE
;
; KEYWORD PARAMETERS:
;       NONE
;
; OUTPUTS:
;       DISCR_STR -- Discrimination structure, defined identically to
;                    same in distance_omnibus.pro
;
; OPTIONAL OUTPUTS:
;       NONE
;
; MODIFICATION HISTORY:
;
;       Created:  10/14/10, TPEB -- Initial Version.
;       Modified: 02/09/11, TPEB -- Error checking code added
;
;-

FUNCTION PROBEVAL_DISCR, d, pvec, dtan
  
  COMPILE_OPT OBSOLETE
  
  ;; Define output structure
  discr = CREATE_STRUCT('nearp',double(0),'farp',double(0),$
                        'neard',double([0,0,0]),'fard',double([0,0,0]))
  
  near = pvec * (d LE dtan)
  far  = pvec * (d GT dtan)
  
  discr.nearp = TOTAL(near)
  discr.farp  = TOTAL(far)
  
  n_cdf = TOTAL( near / discr.nearp, /CUM)
  f_cdf = TOTAL( far  / discr.farp,  /CUM)
  
  ind = WHERE( near EQ max(near), nnd )
  ifd = WHERE( far  EQ max(far),  nfd )
  
  ;; Error checking
  IF nnd EQ 0 THEN nd = 0.d  ELSE nd = d[ind]
  IF nfd EQ 0 THEN fd = 20.d ELSE fd = d[ifd]
  
  IF (nnd GT 1) OR (nfd GT 1) THEN RETURN,discr
  
  nposerr = interpol(d, n_cdf, 0.84134475)
  nnegerr = interpol(d, n_cdf, 0.15865525)
  
  fposerr = interpol(d, f_cdf, 0.84134475)
  fnegerr = interpol(d, f_cdf, 0.15865525)

  discr.neard = [nd, abs(nnegerr-nd), abs(nposerr-nd)]
  discr.fard  = [fd, abs(fnegerr-fd), abs(fposerr-fd)]
  
  RETURN,discr
END
